package com.noble.glamour.ui.user.paygoods;

import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.noble.glamour.R;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.third.recyclerholder.BaseRecyclerViewAdapter;
import com.noble.glamour.third.recyclerholder.BaseRecyclerViewHolder;
import com.noble.glamour.ui.user.paygoods.bean.PayGood;
import com.noble.library.view.CustomFrameLayout;

/**
 * 通用商品列表adapter
 * Created by Su on 2017/3/31.
 */

public class GoodsListAdapter extends BaseRecyclerViewAdapter {
    private int selectPosition = 0;
    private int itemType = 0;       // 展示布局
    private int chargeType = 0;     // 充值种类

    public GoodsListAdapter(int itemType) {
        this.chargeType = itemType;

        if (itemType == GoodsConstant.DLG_VIP_VIDEO_NEW) {
            this.itemType = 2;
        } else if (itemType == GoodsConstant.DLG_DIAMOND_VIDEO) {
            this.itemType = 3;
        } else {
            if (itemType > 0)       // 非0状态下都引用第二种布局
                this.itemType = 1;

            if (itemType == GoodsConstant.DLG_DIAMOND_CHAT) {  // 聊天充值
                this.itemType = 0;
            }
        }
    }

    @Override
    public int[] getItemLayouts() {
        return new int[]{R.layout.p1_goods_list_adapter, R.layout.f1_goods_list_item,
                R.layout.f1_goods_video_item, R.layout.f1_goods_item_new};
    }

    @Override
    public void onBindRecycleViewHolder(BaseRecyclerViewHolder viewHolder, final int position) {
        final PayGood data = (PayGood) getItem(position);

        RelativeLayout payItem = viewHolder.findViewById(R.id.pay_item);
        CustomFrameLayout payBg = viewHolder.findViewById(R.id.pay_bg);
        ImageView img_choose = viewHolder.findViewById(R.id.iv_choose);
        ImageView goods_ico = viewHolder.findViewById(R.id.goods_ico);
        TextView tv_name = viewHolder.findViewById(R.id.tv_name);       // 商品名称
        TextView tv_money = viewHolder.findViewById(R.id.tv_money);     // 商品价格
        TextView tv_desc = viewHolder.findViewById(R.id.tv_desc);       // 商品活动

        // 选中状态
        payItem.setSelected(selectPosition == position);
        payBg.showOfIndex(selectPosition == position ? 1 : 0);

        // 设置数据
        if (!TextUtils.isEmpty(data.getName())) {
            tv_name.setVisibility(View.VISIBLE);
            tv_name.setText(data.getName());
        }

        if (itemType == 1 || itemType == 2) {
            tv_money.setText(Html.fromHtml("&#165;").toString() + data.getPrice());
        } else {
            tv_money.setText(data.getPrice() + "元");
        }

        if (!TextUtils.isEmpty(data.getDesc())) {
            tv_desc.setText(data.getDesc());
        }

        // 充值类型区分
        switch (chargeType) {
            case GoodsConstant.DLG_DIAMOND_CHAT:    // 聊天钻石充值
                goods_ico.setVisibility(View.GONE);
                break;

            case GoodsConstant.DLG_VIP_VIDEO_NEW:
                // 开通季度
                if (position == 0) {
                    String desc = ModuleMgr.getCommonMgr().getCommonConfig().getPay().getQuarterWelfare();
                    tv_desc.setText(TextUtils.isEmpty(desc) ? "" : desc);
                }

                if (position == 1) {
                    String desc = ModuleMgr.getCommonMgr().getCommonConfig().getPay().getMonthWelfare();
                    tv_desc.setText(TextUtils.isEmpty(desc) ? "" : desc);
                }
                break;
        }
    }

    public void updateData(int positon) {
        this.selectPosition = positon;
        notifyDataSetChanged();
    }

    @Override
    public int getRecycleViewItemType(int position) {
        return itemType;
    }
}
